<template>
	<view>
		<u-toast ref="uToast" />
		<view id="bg_box">
			<view @click="open" class="butt">{{ loading?'开启中...':'开启美好生活'}}</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				loading: false
			}
		},
		methods: {
			open() {
				// 判断是否登录，有登录跳转首页，否则跳转选择性别
				if (this.$store.state.Authorization) {
					uni.switchTab({
						url: '/pages/edge/edge'
					})
				} else {

					/* uni.navigateTo({
						url: '/pages/select_sex/select_sex'
					}) */

					this.wx_login()
				}

			},
			wx_login() {

				this.loading = true

				let that = this

				uni.openSetting({
					success(res) {
						console.log(res)

						// 判断是否授权成功
						if (res.authSetting['scope.userInfo']) {

							uni.login({
								provider: 'weixin',
								success(res2) {
									uni.getUserInfo({
										withCredentials: true,
										success(res3) {
											console.log('info:')
											console.log(res3);
											console.log(res3.signature)
											let data = {
													code: res2.code,
													iv: res3.iv,
													rawData: res3.rawData,
													wx_signature: res3.signature,
													encryptedData: res3.encryptedData
												} +
												that.$request({
													url: '/login/wxLogin',
													data: data,
													method: 'post',
													success(res) {
														if (res.statusCode == 200) {

															if (res.data.code == 1) {

															} else {
																that.$refs.uToast.show({
																	title: res.data.msg,
																	type: 'warning'
																})
															}

														} else {
															that.$refs.uToast.show({
																title: '网络繁忙',
																type: 'warning'
															})
														}
													},
													fail(err) {
														that.$refs.uToast.show({
															title: '请求失败',
															type: 'warning'
														})
													},
													complete() {
														that.loading = false
													}
												})

											uni.switchTab({
												url: '/pages/edge/edge'
											})
										},
										fail() {
											console.log("获取用户信息失败")
										}
									})
								},
								fail(err) {

								}
							})

						}
					},
					fail(err) {

						uni.navigateTo({
							url: '/pages/select_sex/select_sex'
						})

					}
				})
			}
		}
	}
</script>

<style scoped>
	#bg_box {
		width: 750rpx;
		height: 100vh;
		background: url('http://wx.90it.com.cn/static/image/open_screen.png');
		background-size: 100% 100%;
	}

	#bg_box .butt {
		width: 620rpx;
		height: 100rpx;
		background-color: #EA4389;
		color: #fff;
		position: fixed;
		bottom: 10vh;
		left: 65rpx;
		border-radius: 100rpx;
		text-align: center;
		line-height: 100rpx;
		font-size: 32rpx;
	}
</style>
